﻿//********************************************************************************//
//创建日期：<创建日期，2016-05-28>
//创建作者：<郭登杰>
//功能说明：来函水表信息列表
//********************************************************************************//
using BLL.Access;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Entity;
using BLL;
using DevExpress.Web;

namespace Web.Controls
{
    public partial class UCLHShuiBiaoList : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        /// 来函流程步骤
        /// </summary>
        public eLaiHanLCBH LaiHanLC {
            get
            {
                return ViewState["LaiHanLC"]==null?eLaiHanLCBH.未登记:(eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), Convert.ToString(ViewState["LaiHanLC"])); 
            }
            set
            {
                ViewState["LaiHanLC"] = value.ToString();
            }        
        }

        /// <summary>
        /// 设置复选框列显隐
        /// </summary>
        public bool ShowCheck;

        /// <summary>
        /// 记录来函编号
        /// </summary>
        public string LaiHanBH 
        {
            get 
            {
                return Convert.ToString(ViewState["LaiHanBH"]);
            }
            set
            {
                ViewState["LaiHanBH"] = value;
            }
        }

        /// <summary>
        /// 设置列表高度
        /// </summary>
        public int ListHeight
        {
            set
            {
                this.gvList.Settings.VerticalScrollableHeight = value;
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {

        }

        /// <summary>
       ///重新绑定
        /// </summary>
        public void BindData()
        {
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            //控制复选框列
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                if (null != cb) cb.Visible = ShowCheck;
            }
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public void ClearInfo()
        {
            this.dtSource = null;
            this.gvList.DataSource = null;
            this.gvList.DataBind();
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            if (this.LaiHanLC < eLaiHanLCBH.登记)
            {
                if (this.LaiHanLX == eLaiHanLX.销户)
                {
                    if (this.dtSource.Rows.Count > 0)
                    {
                        rs.Msg = "当前用户还有水表,不允许销户。";
                        return rs;
                    }
                }
                else if (this.LaiHanLX == eLaiHanLX.销表)
                {
                    if (this.dtSource.Rows.Count == 0)
                    {
                        rs.Msg = "当前用户没有水表,不需要销表。";
                        return rs;
                    }
                    else
                    {
                        List<string> list = this.SelectedShuiBiaoList();
                        if (list.Count == 0)
                        {
                            rs.Msg = "请选择要销的水表。";
                            return rs;
                        }
                        string msg="";
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (BusinessTool.GetShuiBiaoSJZT(list[i],out msg))
                            {
                                rs.Msg = "水表号" + list[i] + msg;
                                return rs;
                            }
                        }
                    }
                }
            }
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 获取所有选中的水表信息
        /// </summary>
        /// <returns></returns>
        private List<string> SelectedShuiBiaoList()
        {
            List<string> list = new List<string>();
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                HiddenField hid = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfShuiBiaoBH");
                if (cb.Checked)
                {
                    list.Add(hid.Value);
                }
            }
            return list;
        }

        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
                if (string.IsNullOrEmpty(laihanxx.S_YONGHUBH))
                {
                    return;
                }
                else
                {
                    if (laihanxx.S_YONGHUBH.Length==6)
                    {
                        laihanxx.S_YONGHUBH = "0" + laihanxx.S_YONGHUBH;
                    }
                }
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH),laihanxx.I_LIUCHENGBH.ToString());
            }
            string sWhere = "'------'";
            DataRowCollection drcTemp = null;
            drcTemp = new YS_CB_YONGHUSBGXBLL().SelectYS_CB_YONGHUSBGXesDynamic(string.Format("I_JLZT=0 and I_LEIXING=1 and S_YONGHUBH='{0}' and I_SHUIBIAOZT=0", laihanxx.S_YONGHUBH), null).Rows;
            //根据用户号加载水表                 
            foreach (DataRow dr in drcTemp)
            {
                sWhere += ",'" + dr["S_SHUIBIAOBH"].ToString() + "'";
            }
            this.dtSource = new YS_CB_SHUIBIAOXXBLL().SelectYS_CB_SHUIBIAOXXesDynamic(String.Format("I_LEIXING=1 and S_SHUIBIAOBH in ({0})", sWhere), null);
            this.BindData();
            //如果为来函销表,勾选销表的项
            if (laihanxx.S_LAIHANBH!=null)
            {
                this.LaiHanBH = laihanxx.S_LAIHANBH;
                //根据来函加载水表
                if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.销表)
                {
                    drcTemp = new YS_LH_XIAOBIAOSBXXBLL().SelectYS_LH_XIAOBIAOSBXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH, null).Rows;
                    Dictionary<string, object> shuibiaohs = new Dictionary<string, object>();
                    foreach (DataRow item in drcTemp)
                    {
                        shuibiaohs.Add(item["S_SHUIBIAOBH"].ToString(), item["S_SHUIBIAOBH"]);
                    }
                    //勾选对应的水表
                    for (int i = 0; i < this.gvList.VisibleRowCount; i++)
                    {
                        ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                        HiddenField hid = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfShuiBiaoBH");
                        cb.Checked = shuibiaohs.ContainsKey(hid.Value);
                        //做过登记的,此时步骤为受理.不可修改了
                        if ((int)this.LaiHanLC < (int)eLaiHanLCBH.登记)
                        {
                            cb.Enabled = true;
                        }
                        else
                        {
                            cb.Enabled = false;
                        }
                    }
                }
            }         
        }        

        /// <summary>
        /// 获取申请信息
        /// </summary>
        /// <returns></returns>
        public List<object> GetShengQingXX()
        {
            List<object> list = new List<object>();
            List<string> shuibiaobhs = this.SelectedShuiBiaoList();
            for (int i = 0; i < shuibiaobhs.Count; i++)
            {
                YS_LH_XIAOBIAOSBXX obj = new YS_LH_XIAOBIAOSBXX();
                obj.S_LAIHANBH = this.LaiHanBH;
                obj.S_SHUIBIAOBH = shuibiaobhs[i];
                list.Add(obj);
            }
            return list;
        }

        /// <summary>
        /// 添加申请信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            //先清除该来函旧的销表水表记录
            YS_LH_XIAOBIAOSBXXBLL bllT2 = new YS_LH_XIAOBIAOSBXXBLL();
            bllT2.DeleteYS_LH_XIAOBIAOSBXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
            //添加销表水表记录
            //设置来函编号
            this.LaiHanBH = laihanxx.S_LAIHANBH;
            List<object> list = this.GetShengQingXX();
            foreach (YS_LH_XIAOBIAOSBXX item in list)
            {
                bllT2.InsertOrUpdate(item);
            }
        }

        #endregion        
    }
}